//
//  NSDictionary+HZY.m
//  FlowerTown
//
//  Created by 何志勇 on 2017/7/21.
//  Copyright © 2017年 TT. All rights reserved.
//

#import "NSDictionary+HZY.h"

@implementation NSDictionary (HZY)
-(NSInteger)toInteger:(id)aKey
{
    NSInteger convertResult=0;
    id obj=[self objectForKey:aKey];
    if (obj && obj!=nil && obj!=[NSNull null]) {
        convertResult=[obj integerValue];
    }
    return convertResult;
}
-(int)toInt:(id)aKey
{
    int convertResult=0;
    id obj=[self objectForKey:aKey];
    if (obj && obj!=nil && obj!=[NSNull null]) {
        convertResult=[obj intValue];
    }
    return convertResult;
}
-(NSString*)toString:(id)aKey
{
    NSString* convertResult=nil;
    id obj=[self objectForKey:aKey];
    if (obj && obj!=nil && obj!=[NSNull null]) {
        convertResult=[NSString stringWithFormat:@"%@",obj];
    }
    return convertResult;
    
}
-(double)toDouble:(id)aKey
{
    double convertResult=0.0000;
    id obj=[self objectForKey:aKey];
    if (obj && obj!=nil && obj!=[NSNull null]) {
        convertResult=[obj doubleValue];
    }
    return convertResult;
    
}
-(float)toFloat:(id)aKey
{
    double convertResult=0.0;
    id obj=[self objectForKey:aKey];
    if (obj && obj!=nil && obj!=[NSNull null]) {
        convertResult=[obj floatValue];
    }
    return convertResult;
}
-(BOOL)hasValueForKey:(id)aKey
{
    if ([self isKindOfClass:[NSDictionary class]]) {
        id obj=[self objectForKey:aKey];
        if (obj && obj!=nil && obj!=[NSNull null]) {
            return YES;
        }
    }
    return NO;
}

- (NSDictionary *)dictionaryByRemoveNULL {
    NSMutableDictionary *resultDict = [NSMutableDictionary dictionary];
    for (NSString *key in self) {
        id value = self[key];
        if ([value isKindOfClass:[NSNull class]]) {
            value = @"";
        } else if ([value isKindOfClass:[NSDictionary class]]) {
            value = [value dictionaryByRemoveNULL];
        } else if ([value isKindOfClass:[NSArray class]]) {
            value = [value arrayByRemoveNULL];
        }
        [resultDict setObject:value forKey:key];
    }
    return resultDict;
}

/// 将字典用&拼接成url形式
- (NSString *)toURLString
{
    NSMutableArray * array = [[NSMutableArray alloc]init];
    for (id key in self) {
        NSString * str = [NSString stringWithFormat:@"%@=%@",key,self[key]];
        [array addObject:str];
    }
    return [array componentsJoinedByString:@"&"];
}

@end

@implementation NSArray (RemoveNULL)

- (NSArray *)arrayByRemoveNULL
{
    NSMutableArray *resultArr = [NSMutableArray array];
    for (id value in self) {
        id item = value;
        if ([item isKindOfClass:[NSNull class]]) {
            item = @"";
        } else if ([item isKindOfClass:[NSArray class]]) {
            item = [item arrayByRemoveNULL];
        } else if ([item isKindOfClass:[NSDictionary class]]) {
            item = [item dictionaryByRemoveNULL];
        }
        
        [resultArr addObject:item];
    }
    return resultArr;
}

@end
